package num98;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author : DuJiabao
 * @Project : sword-finger-offer
 * @Time : 2021/5/9 17:42
 * @Desc :
 */
public class Solution4 {
  public boolean isValidBST(TreeNode root) {
    double[] prev = new double[1];
    prev[0] = -Double.MAX_VALUE;
    return recursion(root, prev);
  }

  public boolean recursion(TreeNode root, double[] prev) {
    if (root == null) return true;
    if (!recursion(root.left, prev)) return false;

    if (prev[0] >= root.val) {
      return false;
    }
    prev[0] = root.val;

    if (!recursion(root.right, prev)) return false;
    return true;
  }

  public static void main(String[] args) {
    TreeNode root = new TreeNode(5);
    root.left = new TreeNode(1);
    root.right = new TreeNode(4);
    root.right.left = new TreeNode(3);
    root.right.right = new TreeNode(6);
    boolean res = new Solution4().isValidBST(root);
    System.out.println(res);
  }
}
